package com.example.coursemanagement.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.coursemanagement.MyUtils.CommonResult;
import com.example.coursemanagement.pojo.Message;
import com.example.coursemanagement.pojo.User;
import com.example.coursemanagement.pojo.UserMessageImg;
import com.example.coursemanagement.service.MessageService;
import com.example.coursemanagement.service.UploadService;
import com.example.coursemanagement.service.UserMessageImgService;
import com.example.coursemanagement.service.impl.UserService;
import com.example.coursemanagement.vo.MessageVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;


@RestController
@RequestMapping("/message" )
@RequiredArgsConstructor
public class MessageController {

    @Resource
    private UploadService uploadService;

    @Resource
    private MessageService messageService;

    @Resource
    private UserMessageImgService userMessageImgService;

    @Resource
    private UserService userService;



    /**
     * 用于上传图片
     * @return
     */
    @RequestMapping(value = "/uploadImg",method = RequestMethod.POST)
    public CommonResult<Object> checkUpdate(@RequestParam("userId") Integer userId,
                                            @RequestParam("messageId") Integer messageId,
                                                MultipartFile img) {
        if (Objects.nonNull(img)) {
            return uploadService.uploadImg(userId, messageId, img);
        }
        return CommonResult.failed("图片为空");
    }

    /**
     * 用于“类朋友圈内容”插入内容后返回messageId,用于后续插入图片设置messageId
     * @return
     */

    @RequestMapping(value = "/content", method = RequestMethod.POST)
    public CommonResult<Object> saveContent(@RequestParam("userId") Integer userId,
                                            @RequestParam("content") String content) {
        if (StringUtils.isNoneBlank(content)) {
            Message message = new Message();
            message.setContent(content);
            message.setUserId(userId);

            // 获取当前时间并设置到 createTime 属性
            LocalDateTime currentTime = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String createTimeString = currentTime.format(formatter);
            message.setCreateTime(createTimeString);

            if (messageService.saveContent(message) > 0) {
                QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("user_id", userId);
                List<Message> messages = messageService.list(queryWrapper);
                Integer generatedId = messages.get(messages.size() - 1).getId();
                return CommonResult.success(generatedId);
            }
        }
        return CommonResult.failed("内容为空");
    }

    /**
     * 用于获取message列表
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<Object> getMessageList() {
        List<Message> messages = messageService.list();
        List<MessageVo> messageVos = new ArrayList<>(); // 初始化 messageVos 列表
        if (messages != null) {
            for (Message message : messages) {
                MessageVo messageVo = new MessageVo();
                messageVo.setId(message.getId());
                messageVo.setContent(message.getContent());
                messageVo.setLikeCount(message.getLikeCount());
                if (message.getCreateTime() == null) {
                    messageVo.setCreateTime("time-null");
                } else {
                    messageVo.setCreateTime(message.getCreateTime());
                }
                User user = userService.getById(message.getUserId());
                if (user != null) {
                    messageVo.setUserName(user.getRealname());
                } else {
                    messageVo.setUserName("null");
                }
                QueryWrapper<UserMessageImg> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("message_id", message.getId());
                UserMessageImg userMessageImg = userMessageImgService.getOne(queryWrapper);
                if (userMessageImg != null) {
                    messageVo.setMessageImg(userMessageImg.getImgPath());
                }
                messageVos.add(messageVo);
            }
            Collections.reverse(messageVos);
        }
        return CommonResult.success(messageVos);
    }

    /**
     * 点赞
     * @return
     */
    @RequestMapping(value = "/like",method = RequestMethod.POST)
    public CommonResult<Object> checkUpdate(@RequestParam("messageId") Integer messageId) {
        if (Objects.nonNull(messageId)) {
            Message message = messageService.getById(messageId);
            message.setLikeCount(message.getLikeCount()+1);
            messageService.updateById(message);
            return CommonResult.success(message.getLikeCount());
        }
        return CommonResult.failed("点赞");
    }


}
